<?php

/**

 * Some people get confused, and think factories/modules ARE stand-alone

 * plugins. If they try to activate this factory as if it was a plugin, they will

 * be shown a message explaining how to install aLinks.

 */

if (!defined('ALINKS_RUNNING')) {

	/**

	Plugin Name: Amazon Basic Factory

	Description: aLinks factory component

	*/

	add_action('admin_notices', create_function(null, '

	?>

	<div id="alinks_notice" class="updated fade-ff0000">

		<p><strong>Error</strong><br />

		You activated an aLinks factory as a plugin. It is a component of the aLinks. It is not a WordPress plugin. You must have aLinks installed to

		use this.</p><p>You can download aLinks from <a href="http://alinks.headzoo.com/download">here</a>. Once you have aLinks installed

		and activated, you can install this factory following the directions in the <a href="http://alinks.headzoo.com/documentation">documentation</a>.</p?

	</div>

	<?php

	'));

	return;

}



/**

 * @see Alinks_Factory_Abstract

 */

require_once('Alinks/Factory/Abstract.php');



/**

 * Amazon Basic Factory

 *

 * A simple Amazon affiliates factory. You define the keyphrase, the

 * product type, and the search term.

 *

 * This class can also be studied to learn how to create factories.

 *

 * @author Sean Hickey <sean@headzoo.com>

 * @version $Id: factory.php 396 2008-07-04 21:44:45Z sean $

 */

class Alinks_Factory_AmazonBasic extends Alinks_Factory_Abstract

{

	/**

	 * The name of the factory

	 *

	 * @var string

	 */

	protected $_name = 'Amazon Basic';

	

	/**

	 * The factory safe name

	 *

	 * @var string

	 */

	protected $_safe_name = 'amazon_basic';

	

	/**

	 * The URL for the factory

	 *

	 * @var string

	 */

	protected $_url = 'http://alinks.headzoo.com/factories/amazon_basic';

	

	/**

	 * A short description of the factory

	 *

	 * @var string

	 */

	protected $_description = 'A simple Amazon affiliates factory.';

	

	/**

	 * The factory authors

	 *

	 * @var array

	 */

	protected $_authors = array('name' => 'Sean Hickey', 'url' => 'http://headzoo.com');

	

	/**

	 * The factory version

	 *

	 * @var float

	 */

	protected $_version = 2.0;

	

	/**

	 * The version of aLinks the factory is compatible with

	 *

	 * @var string

	 */

	protected $_alinks_version = '2.*';

	

	/**

	 * Whether this factory has a settings form

	 *

	 * @var bool

	 */

	protected $_has_settings_form = true;

	

	/**

	 * Whether this factory has a keyphrases form

	 *

	 * @var bool

	 */

	protected $_has_keyphrases_form = true;



	/**

	 * Whether the factory has HTML to display on the Help page

	 *

	 * @var bool

	 */

	protected $_has_help = true;

	

	/**

	 * Returns the form information for the keyphrases page

	 *

	 * @var Alinks_Form

	 */

	public function getKeyphrasesForm()

	{

		$form = new Alinks_Form($this);

		$phrase = new Alinks_Form_Element_Text('phrase', null, 'Phrase: ');

		$phrase->setRequired(true);

		$form->addElement($phrase);

		

		$description = new Alinks_Form_Element_Text('description', null, 'Description: ');

		$form->addElement($description);

		

		$product_types = array(

			'blended' => 'All Products',

			'books' => 'Books',

			'music' => 'Music',

			'dvd' => 'DVD',

			'pc-hardware' => 'Computers',

			'videogames' => 'Computer &amp; Video Games',

			'software' => 'Software',

			'electronics' => 'Electronics',

			'apparel-index&amp;platform=gurupa' => 'Apparel &amp; Accessories',

			'baby' => 'Baby',

			'beauty-index&amp;platform=gurupa' => 'Beauty',

			'photo' => 'Camera &amp; Photo',

			'wireless-phones' => 'Cell Phones &amp; Service',

			'classical' => 'Classical Music',

			'gourmet-index&amp;platform=gurupa' => 'Gourmet Food',

			'garden' => 'Home &amp; Garden',

			'universal' => 'Miscellaneous',

			'hpc-index&amp;platform=gurupa' => 'Health &amp; Personal Care',

			'jewelry-index&amp;platform=gurupa' => 'Jewelry &amp; Watches',

			'kitchen' => 'Kitchen &amp; Housewares',

			'magazines' => 'Magazine Subscriptions',

			'mi-index&amp;platform=gurupa' => 'Musical Instruments',

			'sporting-index&amp;platform=gurupa' => 'Sports &amp; Outdoors',

			'tools' => 'Tools &amp; Hardware',

			'toys' => 'Toys &amp; Games'

		);

		

		$product_type = new Alinks_Form_Element_Select('product_type', null, 'Product Type: ', $product_types);

		$product_type->setDescription('The product category the search will take place in.');

		$form->addElement($product_type);

		

		$search_term = new Alinks_Form_Element_Text('search_term', null, 'Search Term: ');

		$search_term->setDescription('The search term. If none is provided, the phrase will be used as the search term.');

		$form->addElement($search_term);

		

		return $form;

	}

	

	/**

	 * Returns the settings form for this factory

	 *

	 * @return Alinks_Form

	 */

	public function getSettingsForm()

	{

		$form = new Alinks_Form($this);

		$id = new Alinks_Form_Element_Text('associates_id', null, 'Amazon Associates ID');

		$id->setDescription('Your Amazon Associates ID.');

		$id->setRequired(true);

		$form->addElement($id);

		

		$urls = array(

			"amazon.com" => 'amazon.com',

			"amazon.co.uk" => 'amazon.co.uk',

			"amazon.ca" => 'amazon.ca',

			"amazon.de" => 'amazon.de',

			"amazon.co.jp" => 'amazon.co.jp',

			"amazon.fr" => 'amazon.fr'

		);

		

		$url = new Alinks_Form_Element_Select('amazonurl', null, 'Amazon URL: ', $urls);

		$url->setDescription('Clicked Amazon links will go to this Amazon website.');

		$form->addElement($url);

		

		return $form;

	}

	

	/**

	 * Returns HTML to display on the Help page

	 *

	 * @return string

	 */

	public function getHelp()

	{

		return file_get_contents(dirname(__FILE__) . '/help.html');

	}

	

	/**

	 * Returns whether this factory can run

	 *

	 * A factory would typically return false if some required settings

	 * haven't been set by the user yet. When false is returned, any

	 * keyphrases owned by this factory will be ignored.

	 *

	 * @return bool

	 */

	public function canRun()

	{

		/**

		 * We can run so long as the user's associates ID is set

		 */

		return isset($this->_settings['associates_id']);

	}

	

	/**

	 * Replaces a keyphrase with a link

	 *

	 * @param Alinks_Keyphrase_Interface $keyphrase The keyphrase

	 * @param Alinks_LinkBuilder_Interface $builder The link builder

	 * @return Alinks_LinkBuilder_Interface

	 */

	public function parse(Alinks_Keyphrase_Interface $keyphrase, Alinks_LinkBuilder_Interface $builder)

	{

		$amazon_url = (!empty($this->_settings['amazonurl'])) ? $this->_settings['amazonurl'] : 'amazon.com';

		$associates_id = $this->_settings['associates_id'];

		$search_term = ($keyphrase->getOptions('search_term')) ? urlencode($keyphrase->getOptions('search_term')) : urlencode($keyphrase->getPhrase());

		$product_type = ($keyphrase->getOptions('product_type')) ? $keyphrase->getOptions('product_type') : 'blended';

		$url = "http://$amazon_url/exec/obidos/redirect?link_code=ur2&amp;camp=1789&amp;tag=$associates_id&amp;creative=9325&amp;path=external-search%3Fsearch-type=ss%26keyword=$search_term%26index=$product_type";

		$builder->setUri($url);

		

		/**

		 * Amazon requires some crazy invisible image on the same page as the

		 * link. Not sure why, but the links don't work without it.

		 */

		$amazon_image = "http://www.assoc-$amazon_url/e/ir?t=$associates_id&amp;l=ur2&amp;o=1";

		$builder->addPostHtml("<img class=\"amazon_image\" src=\"$amazon_image\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" />");

		

		return $builder;

	}

}